<?php
//By Bing Hua
include_once 'functions.php';
/**
 * return true if record already exists.
 */
function checkCropDesc($cropid, $year) {
	$con = connect_db ();
	$sql = "select * from CROP_DESCRIPTION where cropid=$cropid and year=$year";
	$result = eatResult ( mysql_query ( $sql, $con ) );
	mysql_close ( $con );
	if (count ( $result ) >= 1) {
		return true;
	}
	return false;
}
/**
 * return true if record already exists.
 */
function checkHarvest($cropid, $startDate, $unitid) {
	$con = connect_db ();
	$sql = "select * from AMOUNT_HARVESTED where cropid=$cropid and startdate='$startDate' and unitid=$unitid";
	$result = eatResult ( mysql_query ( $sql, $con ) );
	mysql_close ( $con );
	if (count ( $result ) >= 1) {
		return true;
	}
	return false;
}
/**
 * return true if record already exists.
 */
function checkSeedPurchase($cropid, $startDate) {
	$con = connect_db ();
	$sql = "select * from SEEDS_PURCHASED where cropid=$cropid and startdate='$startDate'";
	$result = eatResult ( mysql_query ( $sql, $con ) );
	mysql_close ( $con );
	if (count ( $result ) >= 1) {
		return true;
	}
	return false;
}
/**
 * return true if record already exists.
 */
function checkPlantAdded($cropid, $startDate) {
	$con = connect_db ();
	$sql = "select * from PLANTS_ADDED where cropid=$cropid and startdate='$startDate'";
	$result = eatResult ( mysql_query ( $sql, $con ) );
	mysql_close ( $con );
	if (count ( $result ) >= 1) {
		return true;
	}
	return false;
}
/**
 * return true if record already exists.
 */
function checkPlantsTransplanted($cropid, $startDate) {
	$con = connect_db ();
	$sql = "select * from PLANTS_TRANSPLANTED where cropid=$cropid and startdate='$startDate'";
	$result = eatResult ( mysql_query ( $sql, $con ) );
	mysql_close ( $con );
	if (count ( $result ) >= 1) {
		return true;
	}
	return false;
}
/**
 * return true if record already exists.
 */
function checkSDLog($cropid, $startDate, $unitid, $sdType, $customerid) {
	$con = connect_db ();
	$sql = "select * from AMOUNT_SOLD_DONATED where cropid=$cropid and startdate='$startDate' and unitid=$unitid and customerid=$customerid and sdType=$sdType";
	$result = eatResult ( mysql_query ( $sql, $con ) );
	mysql_close ( $con );
	if (count ( $result ) >= 1) {
		return true;
	}
	return false;
}

/**
 * Return false if attempt to overwrite existing record.
 * Return true if add new record okay.
 */
function addSeedPurchase($cropid, $quantity, $startDate, $notes) {
	if (checkSeedPurchase ( $cropid, $startDate ) == true) {
		return false;
	}
	$con = connect_db ();
	$sql = "insert into SEEDS_PURCHASED values(
			'$cropid', '$startDate', '$quantity', '$notes')";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
	return true;
}
/**
 * Return false if attempt to overwrite existing record.
 * Return true if add new record okay.
 */
function addPlantAdded($cropid, $quantity, $startDate, $notes) {
	if (checkPlantAdded ( $cropid, $startDate ) == true) {
		return false;
	}
	$con = connect_db ();
	$sql = "insert into PLANTS_ADDED values(
			'$cropid', '$startDate', '$quantity', '$notes')";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
	return true;
}
/**
 * Return false if attempt to overwrite existing record.
 * Return true if add new record okay.
 */
function addPlantsTransplanted($cropid, $quantity, $startDate, $notes) {
	if (checkPlantsTransplanted ( $cropid, $startDate ) == true) {
		return false;
	}
	$con = connect_db ();
	$sql = "insert into PLANTS_TRANSPLANTED values(
			'$cropid', '$startDate', '$quantity', '$notes')";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
	return true;
}
/**
 * Return false if attempt to overwrite existing record.
 * Return true if add new record okay.
 */
function addHarvest($cropid, $quantity, $startDate, $unitid, $notes) {
	if (checkHarvest ( $cropid, $startDate, $unitid ) == true) {
		return false;
	}
	$con = connect_db ();
	$sql = "insert into AMOUNT_HARVESTED values(
			'$cropid', '$startDate', '$quantity', '$unitid', '$notes')";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
	return true;
}
/**
 * Return false if attempt to overwrite existing record.
 * Return true if add new record okay.
 */
function addSDLog($cropid, $quantity, $startDate, $unit, $notes, $sdType, $customerid) {
	if (checkSDLog ( $cropid, $startDate, $unit, $sdType, $customerid ) == true) {
		return false;
	}
	$con = connect_db ();
	$sql = "insert into AMOUNT_SOLD_DONATED values(
			'$cropid', '$startDate', '$quantity', '$notes', '$sdType', '$unit', '$customerid')";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
	return true;
}
/**
 * Return false if attempt to overwrite existing record.
 * Return true if add new record okay.
 */
function addCropDesc($cropid, $year, $notes) {
	if (checkCropDesc ( $cropid, $year ) == true) {
		return false;
	}
	$con = connect_db ();
	$sql = "insert into CROP_DESCRIPTION values(
			'$cropid', '$year', '$notes')";
	if (mysql_query ( $sql, $con )) {
		echo 'add CROP_DESC ok!';
	} else {
		echo 'add CROP_DESC failed!';
	}
	mysql_close ( $con );
	return true;
}

/**
 * make update on existing record.
 */
function updateSeedPurchase($cropid, $quantity, $startDate, $notes) {
	$con = connect_db ();
	$sql = "update SEEDS_PURCHASED
			SET	quantity='$quantity',
				notes='$notes'
			WHERE cropid=$cropid
				AND startDate='$startDate'";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
}
/**
 * make update on existing record.
 */
function updatePlantsAdded($cropid, $quantity, $startDate, $notes) {
	$con = connect_db ();
	$sql = "update PLANTS_ADDED
			SET	quantity='$quantity',
				notes='$notes'
			WHERE cropid=$cropid
				AND startDate='$startDate'";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
}
/**
 * make update on existing record.
 */
function updatePlantsTransplanted($cropid, $quantity, $startDate, $notes) {
	$con = connect_db ();
	$sql = "update PLANTS_TRANSPLANTED
			SET	quantity='$quantity',
				notes='$notes'
			WHERE cropid=$cropid
				AND startDate='$startDate'";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
}
/**
 * make update on existing record.
 */
function updateHarvest($cropid, $quantity, $startDate, $unit, $notes) {
	$con = connect_db ();
	$sql = "update AMOUNT_HARVESTED
			SET	quantity='$quantity',
				notes='$notes'
			WHERE cropid=$cropid
				AND startDate='$startDate'
				AND unitid=$unit";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
}
/**
 * make update on existing record.
 */
function updateSDLog($cropid, $quantity, $startDate, $unit, $notes, $sdType, $customerid) {
	$con = connect_db ();
	$sql = "update AMOUNT_SOLD_DONATED
			SET	quantity='$quantity',
				notes='$notes'
			WHERE cropid=$cropid
				AND startDate='$startDate'
				AND customerid=$customerid
				AND unitid=$unit
				AND sdType=$sdType";
	mysql_query ( $sql, $con );
	mysql_close ( $con );
}
/**
 * make update on existing record.
 */
function updateCropDesc($cropid, $year, $notes) {
	$con = connect_db ();
	$sql = "update CROP_DESCRIPTION
			SET
				notes='$notes'
			WHERE cropid=$cropid
				AND year=$year";
	if (mysql_query ( $sql, $con )) {
		echo 'updates CROP_DESC ok!';
	} else {
		echo 'updates CROP_DESC failed!';
	}
	mysql_close ( $con );
}

function getCropDesc($year, $cropid) {
	$con = connect_db ();
	$sql = "select * from CROP_DESCRIPTION where cropid=$cropid
				AND year=$year";
	$result = eatResult ( mysql_query ( $sql, $con ) );
	mysql_close ( $con );
	return $result [0] ['notes'];
}

function getCropName($string) {
	$result = strtok ( $string, " \n\t" );
	while ( true ) {
		$temp = strtok ( " \n\t" );
		if (! strcmp ( $temp, '-' )) {
			break;
		}
		$result = $result . " " . $temp;
	}
	return $result;
}

function getCropUnit($string) {
	$result = strtok ( $string, " \n\t" );
	while ( true ) {
		$temp = strtok ( " \n\t" );
		if (! strcmp ( $temp, '-' )) {
			break;
		}
	}
	$tok = strtok ( " \n\t" );
	return $tok;
}
?>